package com.ybkj.chat.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ybkj.chat.R;

public class PpwMessageMenu extends PopupWindow {

    private LayoutInflater mInflater;
    private View mView;

    private TextView qr, add_friend, creat_group, add_group, qr_code;

    private OnMenuButtonClickListener mOnMenuButtonClickListener;

    private Context mContext;
    private Activity mActivity;

    public PpwMessageMenu(Context context) {
        super(context);
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        init();
        setPopwindow();
    }

    private void init() {
        mView = mInflater.inflate(R.layout.pop_message_menu, null);

        qr = mView.findViewById(R.id.qr);
        add_friend = mView.findViewById(R.id.add_friend);
        creat_group = mView.findViewById(R.id.creat_group);
        add_group = mView.findViewById(R.id.add_group);
        qr_code = mView.findViewById(R.id.my_qecode);

        qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuButtonClickListener != null) {
                    mOnMenuButtonClickListener.onQrButtonClick();
                }
                dismiss();
            }
        });

        add_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuButtonClickListener != null) {
                    mOnMenuButtonClickListener.onAddFriendButtonClick();
                }
                dismiss();
            }
        });

        creat_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuButtonClickListener != null) {
                    mOnMenuButtonClickListener.onCreatGroupButtonClick();
                }
                dismiss();
            }
        });

        add_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuButtonClickListener != null) {
                    mOnMenuButtonClickListener.onAddGroupButtonClick();
                }
                dismiss();
            }
        });

        qr_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuButtonClickListener != null) {
                    mOnMenuButtonClickListener.onMyQrCodeButtonClick();
                }
                dismiss();
            }
        });

    }

    private void setPopwindow() {
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置pop透明效果
        this.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 设置pop出入动画
        this.setAnimationStyle(R.style.pop_add);
        // 设置pop获取焦点，如果为false点击返回按钮会退出当前Activity，如果pop中有Editor的话，focusable必须要为true
        this.setFocusable(true);
        // 设置pop可点击，为false点击事件无效，默认为true
        this.setTouchable(true);
        // 设置点击pop外侧消失，默认为false；在focusable为true时点击外侧始终消失
        this.setOutsideTouchable(true);
        // 设置pop关闭监听，用于改变背景透明度
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    setBackgroundAlpha(mActivity, 1f);
                }
            }
        });
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        this.mActivity = activity;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public void setOnMenuButtonCLickListener(OnMenuButtonClickListener onMenuButtonClickListener) {
        this.mOnMenuButtonClickListener = onMenuButtonClickListener;
    }

    public interface OnMenuButtonClickListener {
        //扫一扫
        void onQrButtonClick();

        //添加好友
        void onAddFriendButtonClick();

        //创建群聊
        void onCreatGroupButtonClick();

        //分组管理
        void onAddGroupButtonClick();

        //我的二维码
        void onMyQrCodeButtonClick();
    }


}
